
package com.j256.ormlite.db;

import java.sql.SQLException;

import com.j256.ormlite.android.DatabaseTableConfigUtil;
import com.j256.ormlite.field.DataPersister;
import com.j256.ormlite.field.FieldConverter;
import com.j256.ormlite.field.types.DateStringType;
import com.j256.ormlite.misc.VersionUtils;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.DatabaseTableConfig;

/**
 * Sqlite database type information for the Android OS that makes native calls
 * to the Android OS database APIs.
 * 
 * @author graywatson
 */
public class SqliteAndroidDatabaseType extends BaseSqliteDatabaseType implements DatabaseType {

    public SqliteAndroidDatabaseType() {
        // verifies that we have the right version
        VersionUtils.checkCoreVersusAndroidVersions();
    }

    @Override
    public void loadDriver() {
        // noop
    }

    @Override
    public boolean isDatabaseUrlThisType(String url, String dbTypePart) {
        // not used by the android code
        return true;
    }

    @Override
    protected String getDriverClassName() {
        // no driver to load in android-land
        return null;
    }

    @Override
    public String getDatabaseName() {
        return "Android SQLite";
    }

    @Override
    protected void appendDateType(StringBuilder sb, int fieldWidth) {
        // default is to store the date as a string
        appendStringType(sb, fieldWidth);
    }

    @Override
    protected void appendBooleanType(StringBuilder sb, int fieldWidth) {
        // we have to convert booleans to numbers
        appendShortType(sb, fieldWidth);
    }

    @Override
    public FieldConverter getFieldConverter(DataPersister dataPersister) {
        // we are only overriding certain types
        switch (dataPersister.getSqlType()) {
            case DATE:
                return DateStringType.getSingleton();
            default:
                return super.getFieldConverter(dataPersister);
        }
    }

    @Override
    public boolean isNestedSavePointsSupported() {
        return false;
    }

    @Override
    public boolean isBatchUseTransaction() {
        return true;
    }

    @Override
    public <T> DatabaseTableConfig<T> extractDatabaseTableConfig(ConnectionSource connectionSource,
            Class<T> clazz) throws SQLException {
        return DatabaseTableConfigUtil.fromClass(connectionSource, clazz);
    }
}
